<?php $this->_extends('_layouts/basic_layout'); ?>

<?php $this->_block('resource'); ?>

<?php $this->_endblock(); ?>

<?php $this->_block('title'); ?>
	Html Templates
<?php $this->_endblock(); ?>

<?php $this->_block('admin_contents'); ?>

<div class="row-fluid">	
	<a class="btn btn-info" href="<?php echo url('htmltemplates/index');?>">已添加的Html模板</a>
</div>

<div class="row-fluid">		<!-- sortable ui-sortable -->
	<div class="box span12">
		<div class="box-header well">
			<h2><i class="icon-user"></i> 搜索结果 - Html模板</h2>
		</div>
		<div class="box-content">
			<div role="grid">
				<?php if(!empty($htmlTemplatesUnAdded)): foreach($htmlTemplatesUnAdded as $root => $detail): ?>
					<div class="row-fluid"> 	<!-- sortable ui-sortable -->
						<div class="box span12">
							<div class="box-header well">
								<h2><i class="icon-th"></i> <span class="root_folder"><?php echo $root; ?></span></h2>
								<div class="box-icon">
									<a class="btn btn-setting btn-round" href="#"><i class="icon-plus add_btn"></i></a>
									<a class="btn btn-minimize btn-round" href="#"><i class="icon-chevron-up"></i></a>
									<a class="btn btn-close btn-round" href="#"><i class="icon-remove"></i></a>
								</div>
							</div>
							<div class="box-content">
			                  	<div class="row-fluid">
			                        <?php if(!empty($detail['folder'])): ?>
			                        	<?php $detail['folder'] = array_chunk($detail['folder'], 6, true); ?>
			                        	<h5>文件夹:</h5>
			                        	<?php foreach($detail['folder'] as $folders): ?>
				                        	<div class="row-fluid">
					                        	<?php foreach($folders as $folder): ?>
					                        		<div class="span2">
					                        			<span class="label folders"><?php echo $folder; ?></span>
					                        		</div>
					                        	<?php endforeach; ?>
				                        	</div>
			                        	<?php endforeach; ?>
									<?php endif; ?>
									
									<?php 
										if(!empty($detail['file'])):
										$fileClassify = $htmlTemplateModel->fileClassify($detail['file']);
										foreach($fileClassify as $class => $files):
									?>
										<h5><?php echo $class ?>:</h5>
										<?php $files = array_chunk($files, 6, true); ?>
										<?php foreach($files as $filesline): ?>
				                        	<div class="row-fluid">
					                        	<?php foreach($filesline as $file): ?>
					                        			<?php if($class == '图像'): ?>
					                        				<div class="span2 thumbnail files">
						                        				<img src='<?php echo $htmltemp_url . $root . '/' . $file; ?>' alt="<?php echo $file; ?>" />
																<div class="text-center"><?php echo $file; ?></div>
					                        				</div>
					                        			<?php else: ?>
					                        				<div class="span2 files">
					                        					<span class="label label-info"><?php echo $file; ?></span>
					                        				</div>
					                        			<?php endif; ?>
					                        	<?php endforeach; ?>
				                        	</div>
			                        	<?php endforeach; ?>
									<?php endforeach; endif; ?>
			                    </div>                   
							</div>
						</div><!--/span-->
					</div>
				<?php endforeach; else: ?>
					没有找到未添加的模板
				<?php endif; ?>
			</div>
		</div>
	</div>
</div>

<form id="add_block" class="modal hide fade in form-horizontal">
	<div class="modal-header">
		<button data-dismiss="modal" class="close" type="button">×</button>
		<h3 id="add_foldername">asdasd</h3>
	</div>
	
	<div class="modal-body">
		<fieldset>
			<div class="control-group">
				<label for="add_name" class="control-label">模板名称:</label>
				<div class="controls">
					<input class="input-xlarge focused" id="add_name" name="form[name]" type="text" />
				</div>
			</div>
			
			<div class="control-group">
				<label for="add_desc" class="control-label">描述:</label>
				<div class="controls">
					<input class="input-xlarge focused" id="add_desc" name="form[description]" type="text" />
				</div>
			</div>
			
			<div class="control-group">
				<label for="add_rootfolder" class="control-label">根目录文件夹:</label>
				<div class="controls">
					<input class="input-xlarge focused uneditable-input" readonly="readonly" id="add_rootfolder" name="form[root_foldername]" type="text" />
				</div>
			</div>
			
			<div class="control-group">
				<label for="add_thum_s" class="control-label">预览图（小）:</label>
				<div class="controls">
					<select id="add_thum_s" name="form[thumb_small]"></select>
				</div>
			</div>
			
			<div class="control-group">
				<label for="add_thum_b" class="control-label">预览图（大）:</label>
				<div class="controls">
					<select id="add_thum_b" name="form[thumb_big]"></select>
				</div>
			</div>
			
			<div class="control-group">
				<label for="add_previewindex" class="control-label">预览主页:</label>
				<div class="controls">
					<select id="add_previewindex" name="form[preview_indexpage]"></select>
				</div>
			</div>
			
			<div class="control-group">
				<label for="add_downloadpack" class="control-label">下载包:</label>
				<div class="controls">
					<select id="add_downloadpack" name="form[download_pack]"></select>
				</div>
			</div>
			
			<div class="control-group">
				<label for="add_type_area" class="control-label">区域分类:</label>
				<div class="controls">
					<select id="add_type_area" name="form[type_area_id]">
						<?php if(!empty($htmltemplateTypes['type_area'])): foreach($htmltemplateTypes['type_area'] as $type): ?>
							<option value="<?php echo $type['id']; ?>"><?php echo $type['name']; ?>(<?php echo $type['name_en']; ?>)</option>
						<?php endforeach; endif; ?>
					</select>
				</div>
			</div>
			<div class="control-group">
				<label for="add_type_color" class="control-label">颜色分类:</label>
				<div class="controls">
					<select id="add_type_color" name="form[type_color_id]">
						<?php if(!empty($htmltemplateTypes['type_color'])): foreach($htmltemplateTypes['type_color'] as $type): ?>
							<option value="<?php echo $type['id']; ?>"><?php echo $type['name']; ?>(<?php echo $type['name_en']; ?>)</option>
						<?php endforeach; endif; ?>
					</select>
				</div>
			</div>
			<div class="control-group">
				<label for="add_type_columnwidth" class="control-label">行列分类:</label>
				<div class="controls">
					<select id="add_type_columnwidth" name="form[type_columnwidth_id]">
						<?php if(!empty($htmltemplateTypes['type_columnwidth'])): foreach($htmltemplateTypes['type_columnwidth'] as $type): ?>
							<option value="<?php echo $type['id']; ?>"><?php echo $type['name']; ?>(<?php echo $type['name_en']; ?>)</option>
						<?php endforeach; endif; ?>
					</select>
				</div>
			</div>
			<div class="control-group">
				<label for="add_type_sidebarcorner" class="control-label">侧边栏分类:</label>
				<div class="controls">
					<select id="add_type_sidebarcorner" name="form[type_sidebarcorner_id]">
						<?php if(!empty($htmltemplateTypes['type_sidebarcorner'])): foreach($htmltemplateTypes['type_sidebarcorner'] as $type): ?>
							<option value="<?php echo $type['id']; ?>"><?php echo $type['name']; ?>(<?php echo $type['name_en']; ?>)</option>
						<?php endforeach; endif; ?>
					</select>
				</div>
			</div>
			<div class="control-group">
				<label for="add_type_using" class="control-label">用途分类:</label>
				<div class="controls">
					<select id="add_type_using" name="form[type_using_id]">
						<?php if(!empty($htmltemplateTypes['type_using'])): foreach($htmltemplateTypes['type_using'] as $type): ?>
							<option value="<?php echo $type['id']; ?>"><?php echo $type['name']; ?>(<?php echo $type['name_en']; ?>)</option>
						<?php endforeach; endif; ?>
					</select>
				</div>
			</div>
		</fieldset>
	</div>
	
	<div class="modal-footer">
		<a data-dismiss="modal" class="btn cancel" href="#">取消</a>
		<a class="btn btn-primary save" href="#">保存</a>
	</div>
</form>

<script type="text/javascript">
	$('.modal-footer .save').click(function(){
		$.ajax({
			url: '<?php echo url('htmltemplates/searchnew'); ?>',
			type: 'post',
			data: $('#add_block').serialize(),
			success: function(ret){
				if(ret.result){
					$('#add_block').modal( "hide" );
				}else{
					$.each(ret.errorinfo, function(item, errors){
						$.each(errors, function(k, error){
							noty({
								'text': item + ': ' + error,
								'layout': 'topRight',
								'type': 'error'
							});
						});
					});
				}
			},
			dataType: 'json'
		});
	});
	$('.modal-footer .cancel').click(function(){
		$('.currAdd').removeClass('currAdd');
	});

	$('.add_btn').click(function(){
		$('.currAdd').removeClass('currAdd');
		$(this).addClass('currAdd');
		var line = $(this).parent().parent().parent().parent();
		
		var folders = line.find('span.folders');
		var files = line.find('.files');
		var folder_root = line.find('.root_folder').text();

		$('#add_block #add_foldername').text(folder_root);
		$('#add_block #add_name').val(folder_root);
		$('#add_block #add_desc').val('');
		$('#add_block #add_rootfolder').val(folder_root);
		$('#add_block #add_thum_s').html('');
		$('#add_block #add_thum_b').html('');
		$('#add_block #add_previewindex').html('');
		$('#add_block #add_downloadpack').html('');

		var imgExts = new Array('png', 'jpg', 'jpeg', 'bmp', 'gif');
		var indexPages = new Array('index', 'main');
		var pageExts = new Array('php', 'htm', 'html');
		var zipExts = new Array('rar', 'zip');
		$.each(files, function(k, v){
			var value = $(v).text();
			value = value.trim();   
			
			var ext = getFileExt(value);
			if(imgExts.hasVal(ext)){
				var imgObj = new Image();
				imgObj.src = $(v).find('img')[0].src
				
				$('#add_block #add_thum_s').append('<option value="'+value+'">('+imgObj.width+'*'+imgObj.height+')'+value+'</option>');
				$('#add_block #add_thum_b').append('<option value="'+value+'">('+imgObj.width+'*'+imgObj.height+')'+value+'</option>');
			}

			if(pageExts.hasVal(ext)){
				$('#add_block #add_previewindex').append('<option value="'+value+'">'+value+'</option>');
				if(indexPages.hasVal(getFileNameNoExt(value))){
					$('#add_block #add_previewindex').val(value);
				}
			}

			if(zipExts.hasVal(ext)){
				$('#add_block #add_downloadpack').append('<option value="'+value+'">'+value+'</option>');
			}
		});
		$('#addErrors').html('');
		
		$('#add_block').modal('show');
	});
</script>
<?php $this->_endblock(); ?>